# coding: utf-8
from PyQt6.QtWidgets import QFileDialog, QLineEdit
from qfluentwidgets import LineEdit, FluentIcon, Action

from common import SingleIcon


class FolderLineEdit(LineEdit):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.setPlaceholderText("请选择文件夹路径")
        self.openFolderButton = Action(FluentIcon.FOLDER, '', self)

        self.setWindowIcon(SingleIcon.LOGO.icon())

        self.addAction(self.openFolderButton, QLineEdit.ActionPosition.TrailingPosition)
        self.openFolderButton.triggered.connect(self._openFolder)
        self.setClearButtonEnabled(True)
        self.setReadOnly(True)

    def _openFolder(self):
        folder = QFileDialog.getExistingDirectory(self, "选择目录", '')
        if folder:
            self.setText(folder)
